package com.baizhi.controller;

import com.baizhi.common.Constant;
import com.baizhi.entity.Student;
import com.baizhi.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("student")
public class StudentController {
    private StudentService studentService;
    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }
    @RequestMapping("showStudents")
    public String showStudents(Map map, String sname, @RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "2") Integer pageSize){
        PageInfo<Student> pageInfo = studentService.selectStudents(sname, pageNum, pageSize);
        map.put("list",pageInfo.getList());
        map.put("pageNum",pageNum);
        map.put("totalPage",pageInfo.getPages());
        map.put("sname",sname);
//        System.out.println("展示所有的总页数："+pageInfo.getPages());
        return "studentlist";
    }
    @RequestMapping("add")
    public String addStudent(Student s, MultipartFile cover) throws IOException {
        //文件上传
        String oldFileName = cover.getOriginalFilename();//获取上传文件的名字
        String newFileName = UUID.randomUUID().toString()+oldFileName;//上传之后的文件名
        cover.transferTo(new File(Constant.IMG_LOCATION,newFileName));
        //将上传前后的文件名都保存到student对象内  存到数据库
        s.setOldfilename(oldFileName);
        s.setNewfilename(newFileName);
        //调用业务方法
        studentService.insert(s);
        return "redirect:showStudents.do";
    }
    @RequestMapping("removeOne")
    public String removeOneStudent(Integer id){
        studentService.delete(id);
        return "redirect:showStudents.do";
    }
    @RequestMapping("removeAny")
    public String removeAny(Integer[] ids){
        studentService.deleteAny(ids);
        return "redirect:showStudents.do";
    }
    @RequestMapping("getOne")
    public String getOneStudent(Integer id,Map map){
        Student student = studentService.selectById(id);
        map.put("stu",student);
        return "updateStudent";
    }
    @RequestMapping("update")
    public String updateStudent(Integer id,Student s,MultipartFile cover) throws IOException {
        Student stu = studentService.selectById(id);//要更新的对象
        //一、先判断头像是否更新  file.getSize()获取文件的字节长度
        if(cover.getSize()>0){
            //1、说明要更新头像  更新前先将原来的头像从数据库删除
            File file = new File(Constant.IMG_LOCATION,stu.getNewfilename());
            file.delete();
            //2、然后将更新的头像重新上传到数据库
            String oldFileName = cover.getOriginalFilename();//获取更新后要上传头像的文件名字
            String newFileName = UUID.randomUUID().toString()+oldFileName;//更新上传之后的头像名
            cover.transferTo(new File(Constant.IMG_LOCATION,newFileName));
            //3、将上传前后的文件名都保存到要更新的student对象内  存到数据库
            s.setOldfilename(oldFileName);
            s.setNewfilename(newFileName);
        }
        //二、然后调用业务层的更新方法
        studentService.update(s);
        return "redirect:showStudents.do";
    }
    @RequestMapping("download")
    public void downLoad(Integer id,HttpServletResponse response) {
        //先获取要下载的对象
        Student s = studentService.selectById(id);
        //获取要下载的文件名  上传时保存的加UUID的名字
        String newFileName = s.getNewfilename();
        //获取要下载文件当时上传的文件名（不带UUID的） 当作下载后的文件的名字
        String oldFileName = s.getOldfilename();
        try {
            //1、设置响应头信息  将下载后的文件名设置进去
            response.setHeader("content-disposition","attachment;filename="+
                    URLEncoder.encode(oldFileName,"utf-8"));
            //2、利用文件复制工具  进行文件下载
            //要下载的文件
            File file = new File(Constant.IMG_LOCATION,newFileName);
            FileUtils.copyFile(file,response.getOutputStream());
            //不报异常  说明下载成功  将下载量增加1 更新该对象
            s.setDowncount(s.getDowncount()+1);
            studentService.update(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
